package cn.dy.sbo.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import cn.dy.sbo.core.UserManager;
import cn.dy.sbo.model.User;
import cn.dy.sbo.util.CommonUtils;

import com.opensymphony.xwork2.ActionSupport;

public class ModifypassAction extends ActionSupport
{
    private static final long serialVersionUID = 1L;

    private String            oldPass;

    private String            newPass;

    private User              user;

    private Long              userId;

    public void setUserId(Long userId)
    {
        this.userId = userId;
    }

    public User getUser()
    {
        return user;
    }

    public void setOldPass(String oldPass)
    {
        this.oldPass = oldPass;
    }

    public void setNewPass(String newPass)
    {
        this.newPass = newPass;
    }

    @Autowired
    private UserManager userManager;

    public String getOldPass()
    {
        return oldPass;
    }

    public String getNewPass()
    {
        return newPass;
    }

    public String checkPassWord() throws Exception
    {
        if (userManager.isTrue(userId, oldPass))
        {
            Struts2Utils.renderText("true");
        }
        else
        {
            Struts2Utils.renderText("false");
        }
        return null;

    }

    public String show() throws Exception
    {
        user = userManager.getByUserName(CommonUtils.getCurUserName());
        return "show";
    }

    @Override
    public String execute() throws Exception
    {
        user = userManager.getByUserName(CommonUtils.getCurUserName());
        oldPass = CommonUtils.encryptMD5(oldPass);
        if (user.getPassword().equals(oldPass))
        {
            newPass = CommonUtils.encryptMD5(newPass);
            user.setPassword(newPass);
            userManager.save(user);
        }
        else
        {
            return ERROR;
        }
        return "succ";
    }
}